#!/usr/bin/env python

import time

from kivy.logger import Logger
from kivy.uix.tabbedpanel import TabbedPanelHeader

from amgineirc.view.messageView import MessageView

__all__ = ['ServerTabbedPanelHeaderViewModel',
           'ChannelTabbedPanelHeaderViewModel',
           'PrivmsgTabbedPanelHeaderViewModel',
           'DccTabbedPanelHeaderViewModel']

class _OpenTouchTabbedPanelHeaderViewModel(object):

    close_command = 'Close'

    def __init__(self, view, panel, commands_connected, commands_not_connected):
        self._view = view
        self._panel = panel
        self._commands_connected = commands_connected
        self._commands_not_connected = commands_not_connected

    @property
    def connected(self):
        return self._view.content.view_model.connected

    def handle_on_touch_open(self, *largs):
        commands = self._commands_connected if self.connected else self._commands_not_connected
        self._view.select_options(self._view._target,
                                  commands,
                                  self.handle_command)

    def handle_command(self, command, *largs):
        # override this method
        pass

    def _remove_this_page(self):
        self._panel.view_model.remove_connection_page(self._view.content.view_model.server,
                                                      self._view.content.view_model.target)


class ServerTabbedPanelHeaderViewModel(_OpenTouchTabbedPanelHeaderViewModel):

    # primary commands
    channels_command = 'Channels'
    trusted_command = 'Trusted Users Online'
    quit_close_command = 'Quit and Close'
    
    def __init__(self, view, panel):
        _OpenTouchTabbedPanelHeaderViewModel.__init__(self, view, panel,
                                                      [ServerTabbedPanelHeaderViewModel.channels_command,
                                                       ServerTabbedPanelHeaderViewModel.trusted_command,
                                                       ServerTabbedPanelHeaderViewModel.quit_close_command],
                                                      [_OpenTouchTabbedPanelHeaderViewModel.close_command])
        
    def handle_command(self, command, *largs):
        if command == ServerTabbedPanelHeaderViewModel.channels_command:
            # channels
            self._view.content.view_model.th_display_channels()
        elif command == ServerTabbedPanelHeaderViewModel.trusted_command:
            # trusted users
            self._view.content.view_model.th_show_trusted()
        else:
            # close or quit and close
            if self._view.content.view_model.connected: 
                self._view.content.view_model.connection.disconnect()
            else:
                self._remove_this_page()

class ChannelTabbedPanelHeaderViewModel(_OpenTouchTabbedPanelHeaderViewModel):

    # primary commands
    topic_command = 'Topic'
    users_command = 'Users'
    part_close_command = 'Part and Close'
    
    def __init__(self, view, panel):
        _OpenTouchTabbedPanelHeaderViewModel.__init__(self, view, panel,
                                                      [ChannelTabbedPanelHeaderViewModel.users_command, ChannelTabbedPanelHeaderViewModel.part_close_command],
                                                      [_OpenTouchTabbedPanelHeaderViewModel.close_command])
        
    def handle_command(self, command, *largs):
        if command == ChannelTabbedPanelHeaderViewModel.topic_command:
            # display motd in popup
            # get the topic from the server connection vm
            pass
        elif command == ChannelTabbedPanelHeaderViewModel.users_command:
            # displa users
            self._view.content.view_model.th_display_users()
        else:
            self._remove_this_page()

class PrivmsgTabbedPanelHeaderViewModel(_OpenTouchTabbedPanelHeaderViewModel):

    def __init__(self, view, panel):
        _OpenTouchTabbedPanelHeaderViewModel.__init__(self, view, panel,
                                                      [_OpenTouchTabbedPanelHeaderViewModel.close_command],
                                                      [_OpenTouchTabbedPanelHeaderViewModel.close_command])
        
    def handle_command(self, command, *largs):
        if command == _OpenTouchTabbedPanelHeaderViewModel.close_command:
            self._remove_this_page()

class DccTabbedPanelHeaderViewModel(_OpenTouchTabbedPanelHeaderViewModel):

    new_rule_command = 'New Rotary Encryption Rule'
    disconnect_close_command = 'End DCC Connection and Close'

    def __init__(self, view, panel):
        _OpenTouchTabbedPanelHeaderViewModel.__init__(self, view, panel,
                                                      [DccTabbedPanelHeaderViewModel.new_rule_command,
                                                       DccTabbedPanelHeaderViewModel.disconnect_close_command],
                                                      [_OpenTouchTabbedPanelHeaderViewModel.close_command])
        
    def handle_command(self, command, *largs):
        if command == DccTabbedPanelHeaderViewModel.new_rule_command:
            self._view.content.view_model.th_confirm_send_new_rule()
        elif command == DccTabbedPanelHeaderViewModel.disconnect_close_command \
        or command == _OpenTouchTabbedPanelHeaderViewModel.close_command:
            # one of two close commands 
            self._remove_this_page()
